package cn.yuanqiao.web.controller.archive.appManager;

import cn.yuanqiao.archive.appManager.domain.YqTyc;
import cn.yuanqiao.archive.appManager.service.IYqTycService;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 同义词Controller
 *
 * @author yuanqiao
 * @date 2023-06-14
 */
@RestController
@RequestMapping(path = {"/archive/yqTyc","/manage/yqTyc"})
public class YqTycController extends BaseController
{
    @Autowired
    private IYqTycService yqTycService;

    /**
     * 查询同义词列表（分页）
     */
    @PreAuthorize("@ss.hasAnyPermi('archive:yqTyc:list,tenantManage:templateManage:yqTyc:list')")
    @GetMapping("/list")
    public TableDataInfo list(YqTyc yqTyc)
    {
        startPage();
        List<YqTyc> list = yqTycService.selectYqTycList(yqTyc);
        return getDataTable(list);
    }

    /**
     * 查询同义词列表（全部）
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqTyc:list')")
    @GetMapping("/listAll/{primary_word_array}")
    public List<YqTyc> listAll(@PathVariable String[] primary_word_array)
    {
        List<YqTyc> list = yqTycService.selectYqTycListAll(primary_word_array);
        return list;
    }

    /**
     * 获取同义词详细信息
     */
    @PreAuthorize("@ss.hasAnyPermi('archive:yqTyc:query,tenantManage:templateManage:yqTyc:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yqTycService.selectYqTycByID(id));
    }

    /**
     * 新增同义词
     */
    @PreAuthorize("@ss.hasAnyPermi('archive:yqTyc:add,tenantManage:templateManage:yqTyc:add')")
    @Log(title = "同义词", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody YqTyc yqTyc)
    {
//        String message = yqTycService.insertYqTyc(yqTyc);
        return success(yqTycService.insertYqTyc(yqTyc));
//        return toAjax(yqTycService.insertYqTyc(yqTyc));
    }

    /**
     * 修改同义词
     */
    @PreAuthorize("@ss.hasAnyPermi('archive:yqTyc:edit,tenantManage:templateManage:yqTyc:edit')")
    @Log(title = "同义词", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody YqTyc yqTyc)
    {
//        return toAjax(yqTycService.updateYqTyc(yqTyc));
        return success(yqTycService.updateYqTyc(yqTyc));
    }

    /**
     * 删除同义词
     */
    @PreAuthorize("@ss.hasAnyPermi('archive:yqTyc:remove,tenantManage:templateManage:yqTyc:remove')")
    @Log(title = "同义词", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yqTycService.deleteYqTycByIDs(ids));
    }

    /**
     * 修改同义词启用状态
     */
    @PreAuthorize("@ss.hasAnyPermi('archive:yqTyc:editStatus,tenantManage:templateManage:yqTyc:editStatus')")
    @Log(title = "同义词", businessType = BusinessType.UPDATE)
    @PutMapping("/editStatus")
    public AjaxResult editStatus(@Validated @RequestBody YqTyc yqTyc)
    {
        return toAjax(yqTycService.updateYqTycStatus(yqTyc));
    }

    /**
     * 导出同义词列表
     */
    @PreAuthorize("@ss.hasAnyPermi('archive:yqTyc:export,tenantManage:templateManage:yqTyc:export')")
    @Log(title = "同义词", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Long[] ids)
    {
        List<YqTyc> list = yqTycService.exportYqTycListByIDs(ids);
        ExcelUtil<YqTyc> util = new ExcelUtil<YqTyc>(YqTyc.class);
        util.exportExcel(response, list, "同义词数据");
    }

    /**
     * 下载模板
     */
    @PreAuthorize("@ss.hasAnyPermi('archive:yqTyc:importTemplate,tenantManage:templateManage:yqTyc:importTemplate')")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<YqTyc> util = new ExcelUtil<YqTyc>(YqTyc.class);
        util.importTemplateExcel(response, "同义词数据");
    }

    /*
     * 导入同义词列表
     * @author sy
     * @date 2023/6/14 17:31
     */
    @PreAuthorize("@ss.hasAnyPermi('archive:yqTyc:import,tenantManage:templateManage:yqTyc:import')")
    @Log(title = "同义词", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importYQTYC(MultipartFile file, Boolean updateSupport) throws Exception
    {
        if(file.isEmpty()){
            AjaxResult.error("参数错误！");
        }
        ExcelUtil<YqTyc> util = new ExcelUtil<>(YqTyc.class);
        List<YqTyc> tycLists = util.importExcel(file.getInputStream());
        return yqTycService.importYQTYC(tycLists,updateSupport);
    }
}
